local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.BaseComponent());
local CommonOHOH = _G.CommonOHOH;
local RecipeTool=_G.CommonOHOH.load.RecipeTool();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	print("deco ctor")
	-- local decoFolder= Instance.new("Folder",self.parent.main_inst)
	-- decoFolder.Name="Decoration"
	-- self.decoParent=decoFolder
	self.Database= self.parent.player:GetComponent(ServerOHOH.load.Server_PlayerDataComponent());
	--self.AttachmentList={}
	self.DecoList = {}
	--监听装饰发生变化（）
	self.parent.player.DecoEuqipUpdateEvent:Connect(function()
		self:UpdateDecoration()
	end)
	self:UpdateDecoration()
end;

function M.dtor(self)
	M.super.dtor(self);
end;

function M.ChangeRngData(self,rCfg,mul)
	if not self.RngDataCtrl then
        self.RngDataCtrl = self.parent.player:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    end

	for i = 1, #rCfg.property do
 		local value = rCfg.value[i] 
		local valType =rCfg.valueType[i]
		
		if valType == 1 then
			self.RngDataCtrl:AddLucky(value*mul)
		elseif valType == 2 then
			self.RngDataCtrl:AddShinyLucky(value*mul)
		elseif valType == 3 then
			self.RngDataCtrl:AddRainbowLucky(value*mul)
		elseif valType == 4 then
			self.RngDataCtrl:AddVoidLucky(value*mul)
		elseif valType == 5 then
			self.RngDataCtrl:AddRollSpeed(value*mul)
		end
	end
	
end

function M.UpdateDecoration(self)
	local now=self.Database:GetData("recipe").equip.__real

	if self.oldEquip then
		local isEqual=true
		for k = 1, #now do
			if not(self.oldEquip[k] and self.oldEquip[k]==now[k]) then
				isEqual=false
				break
			end
		end
		if isEqual then
			for k = 1,#self.oldEquip do
				if not(now[k] and now[k]==self.oldEquip[k]) then
					isEqual=false
					break
				end
			end
		end
		if isEqual then
			return
		end
		for i = 1, #self.oldEquip do
			--减少buff值
				local cfg = RecipeTool.GetRecipeCfgData(self.oldEquip[i])
				self:ChangeRngData(cfg,-1)
		end
	end
	
	for i,model in pairs(self.DecoList) do
		self.DecoList[i]:Destroy()
		self.DecoList[i]=nil
	end
	-- self.decoParent:ClearAllChildren()

	task.wait(0.5)

	self.oldEquip={}
	--local objList={}
	if #now == 0 then
		return
	end
	for i = 1, #now do
		self.oldEquip[i] = now[i]
		self.DecoList[i]= RecipeTool.GetRecipeModel(now[i]):Clone()
		self.DecoList[i].Parent = self.parent.player.character.main_inst
	--增加buff值
		local cfg = RecipeTool.GetRecipeCfgData(now[i])
		self:ChangeRngData(cfg,1)
	end

	-- --装饰新物品
	-- self.decoList[rCfg.itemType] = {
	-- 	id = rId,
	-- 	model = RecipeTool.GetRecipeModel(rId)
	-- }
	-- self.decoList[rCfg.itemType].model.Parent = self.player.character.main_inst
	

	-- for k,v in pairs(now) do
	-- 	self.oldEquip[k]=v
	-- 	for i=1,v do
	-- 		local model= PetTool.GetPetModelByPetId(k)
	-- 		table.insert(objList,model)
	-- 	end
	-- end

	-- if #objList==0 then
	-- 	return
	-- end

	--local posList=workspace.Pets.Pos:FindFirstChild(#objList):GetDescendants()
	
	-- for k,v in pairs(objList) do
		
		
	-- end

end




return M